package com.cxb.demotest.mq.rocketmq.batch;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * 批量消息生产者
 * @author Xb
 */
public class BatchProducer {
    public static void main(String[] args) throws Exception {
        // 实例化消息生产者
        DefaultMQProducer producer = new DefaultMQProducer("BatchProducer");

        // 设置nameserver的地址
        producer.setNamesrvAddr("127.0.0.1:9876");

        // 启动producer实例
        producer.start();

        List<Message> messageList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            // 创建消息，执行topic，tag，消息体
            Message message = new Message(
                    "BatchTopic",
                    "Tag",
                    "orderID000" + i,
                    ("Hello, batchMessage!"+i).getBytes()
            );
            messageList.add(message);

        }

        // 发送消息到broker
        SendResult sendResult = producer.send(messageList);

        // 关闭生产者实例
        producer.shutdown();

    }

}
